package com.candy.databasedocuments.service.database;

import com.candy.databasedocuments.service.TableMateSqlService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author suYanLong
 * @version 1.0
 * @date 2023/7/5 15:57
 * @Description msql查询sql
 */
@Service
public class MysqlTableMateSqlServiceImpl implements TableMateSqlService {

    @Override
    public String getDateMateName() {
        return "select database()";
    }

    @Override
    public String getDateMateTableName() {
        return "SELECT TABLE_NAME as tableName,TABLE_COMMENT as tableComment FROM INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA = ?";
    }

    @Override
    public String selectTables(List<String> size) {
        String sql = "SELECT table_name as tableName, COLUMN_NAME as colName, COLUMN_COMMENT as note, COLUMN_TYPE as type, CASE WHEN IS_NULLABLE = 'YES' THEN 'Y' ELSE '' END AS ifNull, IF (column_key = 'pri', 'PK', '') as rule, COLUMN_DEFAULT as columnDefault, EXTRA as extra FROM INFORMATION_SCHEMA. COLUMNS WHERE table_name in ";
        StringBuilder builder = new StringBuilder();
        builder.append("(");
        for (int i = 0; i < size.size(); i++) {
            if (i == size.size() - 1) {
                builder.append("?");
            } else {
                builder.append("?,");
            }
        }
        builder.append(")");
        builder.append(" and table_schema = ?");
        sql = sql + builder;
        return sql;
    }

    @Override
    public String selectKeys(String tableName) {
        String sql = "show keys from " + tableName;
        return sql;
    }

}
